#include <iostream>
using namespace std;

#include <vector>
#include <unordered_map>

class Solution {
public:
    int findMaxLength(vector<int>& nums)
    {
        unordered_map<int, int> map;
        map[0] = -1;

        int sum = 0, ret = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            sum += nums[i] == 0 ? -1 : 1;
            if (map.count(sum))
                ret = max(ret, i - map[sum]);
            else
                map[sum] = i;
        }

        return ret;
    }
};

int main()
{
    Solution s;
    vector<int> v = { 1,0,1,1,0,1,0,0,1,0 };
    auto ret = s.findMaxLength(v);
    cout << ret << endl;
    return 0;
}